/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cooper.classes.dao;

/**
 *
 * @author Bruno
 */

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class ConnectionCooperFactory {

    public static Connection getConnection() throws CooperDAOException {
        try {
          String url = "jdbc:mysql://localhost/cooperativas";
          Class.forName ("com.mysql.jdbc.Driver").newInstance ();
          Connection conn = DriverManager.getConnection (url, "root", "root");  
           return conn;
        } catch (Exception e) {
            throw new CooperDAOException(e.getMessage());
        }
    }

    public static void closeConnection(Connection conn, Statement stmt, ResultSet rs) throws CooperDAOException {
        close(conn, stmt, rs);
    }

    public static void closeConnection(Connection conn, Statement stmt)
            throws CooperDAOException {
        close(conn, stmt, null);
    }

    public static void closeConnection(Connection conn)
            throws CooperDAOException {
        close(conn, null, null);
    }

    private static void close(Connection conn, Statement stmt, ResultSet rs)
            throws CooperDAOException {
        try {
            if (rs != null) {
                rs.close();
            }
            if (stmt != null) {
                stmt.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (Exception e) {
            throw new CooperDAOException(e.getMessage());
        }
    }
}
